<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="edu.whu.sang.kps.search.graphStruct.SimpleAnswer" %>
<%@page import="edu.whu.sang.kps.search.QueryManager" %>
<%@page import="java.util.HashMap" %>

<html lang='zh-CN' xml:lang='zh-CN' xmlns='http://www.w3.org/1999/xhtml'>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <meta http-equiv="Content-Language" content="zh-CN"/>

<link  href="my-style.css"  rel="stylesheet" type="text/css">
</head>

<body>

<DIV id= "myTitle">
Find Grape
</DIV>

<%String keyword = (String)request.getAttribute("returnKey"); 
	 if(keyword == null){
	 	keyword = "";
	 }
%>


<DIV id="banner_search">
	<FORM  action="/WebIndexSearch/FindResult">
		<INPUT class = "text_input" value="<%=keyword%>" name="keyword"> 
		<INPUT class="search_button" type="submit" value="Search"> 
	</FORM>
</DIV>

<hr>
<% String time=(String)request.getAttribute("time");
		if(time !=null){%>
			<%="Time exhausted: "+time%>
<%		}%>



<DIV id=result_content>
<% 
	SimpleAnswer[] sa =(SimpleAnswer[]) request.getAttribute("result"); 
	
	String[]  keywords = null;
	int k =50;
	if(sa != null&&keyword!=null){
		QueryManager qm = QueryManager.getInstance();
			keywords = keyword.split(" "); 
			double sum = 0.0d;
			int i = 0;
			int pathSum = 0;
			for (; i < sa.length; i++) {
				SimpleAnswer at = sa[i];
				if (at == null) {
					break;
				}%>
				<%="<h4>Grape No." + (i + 1) + ": {" + at.getScore() + "}</h4> "%>
				<% sum += at.getScore();
				int[][] v = at.getTreeArray();
				int[][] res = at.change(v);
				HashMap<Integer,Integer> edge = at.getNextForEdge(v);%>
				<table border="0" cellspacing=0   cellpadding=0  style="padding:0px 0px; height:80%"><!--    -->
					<%if(res!=null && edge!=null){
						for(int x=0;x<res.length;x++){%>
						<tr>
					<% 		for(int y=0;y<res[x].length;y++){%>								
					<% 		 switch(res[x][y]){
								 case -2:  //xx  %>
								 		<td class="td_show" >								 		
					 <%   				if(y%2 != 0 && (x+1)<res.length){//
												if(res[x+1][y] == -3 || res[x+1][y] == -4){//形如"T"%>
													<table  border="0"  cellpadding=0 cellspacing=0 style="border:none;height:100%;width:33px">
														<tr>
															<td style=" border-bottom:blue solid 1px; "></td>
															<td style=" border-bottom:blue solid 1px; "></td>
														</tr>
														<tr>
															<td style="border-top:blue solid 1px;border-right:blue solid 1px;"></td>
															<td style="border-top:blue solid 1px;border-left:blue solid 1px;"></td>
														</tr>
													</table>
						<%					}else{
														int m=0;
														for(;m<res.length;m++){
															if(res[m][y] == -3 ||  res[m][y] == -4 || res[m][y] >0){
																break;
															}
														}
														if(m>=res.length){%>
															<table  border="0"  cellpadding=0 cellspacing=0 style="border:none;height:100%;width:32px">
																<tr>
																	<td style=" border-bottom:blue solid 1px; "></td>
																</tr>
																<tr>
																	<td style=" border-top:blue solid 1px; "></td>
																</tr>
															</table>
										<%			}else{%>
															<table  border="0"  cellpadding=0 cellspacing=0 style="border:none;height:100%;width:100%">
																<tr>
																	<td style=" border-bottom:blue solid 1px; "></td>
																</tr>
																<tr>
																	<td style=" border-top:blue solid 1px; "></td>
																</tr>
															</table>
										<%			}
											}
										}else{%>
														<table  border="0"  cellpadding=0 cellspacing=0 style="border:none;height:100%;width:100%">
																<tr>
																	<td style=" border-bottom:blue solid 1px; "></td>
																</tr>
																<tr>
																	<td style=" border-top:blue solid 1px; "></td>
																</tr>
															</table>
						<%				}									
									break;
								case -3://xy%>
										<td class="td_show" >
					<%				if(y%2 != 0 && (x+1)<res.length){
										if(res[x+1][y] == -3 || res[x+1][y] == -4){//类似"˧"这种类型%>
											<table  border="0"  cellpadding=0 cellspacing=0 style="border:none;height:100%;width:100%">
												<tr>
													<td style=" border-bottom:blue solid 1px; border-right:blue solid 1px;"></td>
													<td style=" border-left:blue solid 1px;"></td>
												</tr>
												<tr>
													<td style="border-top:blue solid 1px;border-right:blue solid 1px;"></td>
													<td style=" border-left:blue solid 1px;"></td>
												</tr>
											</table>
					<%					}else{//类似"˩"这种类型%>
											<table  border="0"  cellpadding=0 cellspacing=0 style=" border:none;height:100%;width:100%">
												<tr>
													<td style=" border-bottom:blue solid 1px; border-right:blue solid 1px;"></td>
													<td style=" border-left:blue solid 1px;"></td>
												</tr>
												<tr>
													<td style="border-top:blue solid 1px;"></td>
													<td></td>
												</tr>
											</table>
					<%					}
									}else{//类似"˩"这种类型%>
										<table  border="0"  cellpadding=0 cellspacing=0 style=" border:none;height:100%;width:100%">
												<tr>
													<td style=" border-bottom:blue solid 1px; border-right:blue solid 1px;"></td>
													<td style=" border-left:blue solid 1px;"></td>
												</tr>
												<tr>
													<td style="border-top:blue solid 1px;"></td>
													<td></td>
												</tr>
										</table>
					<%				}				
									break;
								case -4://yy,竖线"|" %>
										<td class="td_show" >
										<table  border="0"  cellpadding=1 cellspacing=0 style="border:none;height:100%;width:33px;">
											<tr>
												<td style=" border-right:blue solid 1px"></td>
												<td style=" border-left:blue solid 1px"></td>
											</tr>
											<tr>
												<td style=" border-right:blue solid 1px"></td>
												<td style=" border-left:blue solid 1px"></td>
											</tr>
										</table>
					<%				break;
								case 0:%>
										<td>
										<%=" "%>
					<%				break;
								default:%>
										<td >
					<%					String temp = qm.getVertexContent(res[x][y]);
										String[] strs = temp.split("@@");
										if(strs.length >= 2){
											//if(strs[1].length()>50){
											//	strs[1] = strs[1].substring(0,50);
											//}
											int begin = strs[1].indexOf(":");
											temp = strs[0]+"@@"+strs[1].substring(begin+1);
										}
										String rel="";
										StringBuffer sb =new StringBuffer(temp);
										int index=0;
										for(int n=0;n<keywords.length;n++){
											 index=0;
										   while(true){
											   index = sb.toString().toLowerCase().indexOf(keywords[n].toLowerCase(), index);
											   if(index ==-1) break;
											   sb.insert(index+keywords[n].length(),"</span>");
											   sb.insert(index, "<span class=\"text_show\">");
											   index += keywords[n].length()+"</span>".length()+"<span class=\"text_show\">".length();
										   }
										}
					%>
										<div id="div_show">
										<%if(edge.get(res[x][y]) != null){
												rel = qm.getEdgeRelation(res[x][y]+"-->"+edge.get(res[x][y]));
												if(rel == null){
													rel = qm.getEdgeRelation(edge.get(res[x][y])+"-->"+res[x][y]);
													if(rel != "" ){
														rel = "<div class=\"rel_show\">"+rel+"←"+"</div></div></a>";
													}
												}else{
													rel = "<div class=\"rel_show\">"+rel+"→"+"</div></div></a>";
												}
											  	
										    }else{
												rel = "</div></a>";
											}
										    
										    index = sb.toString().toLowerCase().indexOf("@@", 0);
										    sb.insert(index,rel);
										    sb.insert(index,"</strong>&nbsp;&nbsp;&nbsp;");
										    sb.insert(0,"<a href=\"/WebSearch/MoreInfo?id="+res[x][y]+"\"><div title=\"more information\"><strong>");
										    %>
										<%=sb.toString().replaceAll("@@", "<hr>")%>
										</div>
					<%				break;
								}%>
							</td>
					<% 		}	%>
						</tr>		
					<%	} 
					}%>
					
					</table>				
			<%}
			if (i == 0) {%>
				<%="Sorry, no result is found."%>
			<%}
			else {%>
				<%="Average score: " + (sum / Math.min(k, i))%>
			<%}
	}%>


</DIV>
</body>
</html>